package com.example.gkcircle.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.avos.avoscloud.AVUser;
import com.example.gkcircle.R;
import com.example.gkcircle.bean.Love;
import com.example.gkcircle.utils.UniversalImageLoader;

import java.util.List;

/**
 * 表白墙
 * Created by Jehu on 16/3/18.
 */
public class LoveAdapter extends BaseAdapter {


    private List<Love> mData;
    private Activity mActivity;
    private LayoutInflater inflater;
    private final int ANONYMOUS_TYPE = 0;// 匿名布局
    private final int UN_ANONYMOUS_TYPE = 1;// 不匿名布局
    private final int MAX_TYPE_LAYOUT = 2;

    public LoveAdapter(Activity activity, List<Love> data) {
        this.mData=data;
        this.mActivity = activity;
        this.inflater = LayoutInflater.from(mActivity);
    }

    @Override
    public int getItemViewType(int position) {
        if(mData.get(position).isAnonymous()){
            return ANONYMOUS_TYPE;
        }else{
            return UN_ANONYMOUS_TYPE;
        }
    }

    @Override
    public int getViewTypeCount() {
        return MAX_TYPE_LAYOUT;
    }

    @Override
    public int getCount() {
        return mData==null?0:mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    static class AnonymousViewHolder{
        ImageView avatar;
        TextView username;
        TextView date;
        TextView content;
    }

    static class UnanonymousViewHolder{
        ImageView avatar;
        TextView username;
        TextView date;
        TextView content;
        TextView commentCount;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        int currentPosition=getItemViewType(position);
        //匿名布局
        Love love=mData.get(position);

        if(currentPosition==ANONYMOUS_TYPE){
            final AnonymousViewHolder anonymousViewHolder;
            if(convertView==null){
                anonymousViewHolder=new AnonymousViewHolder();
                convertView = inflater.inflate(R.layout.love_love_anonymous_item, null,false);
                //初始化控件
                anonymousViewHolder.avatar=(ImageView)convertView.findViewById(R.id.love_love_anonymous_item_avatar);
                anonymousViewHolder.username=(TextView)convertView.findViewById(R.id.love_love_anonymous_item_username);
                anonymousViewHolder.date=(TextView)convertView.findViewById(R.id.love_love_anonymous_item_date);
                anonymousViewHolder.content=(TextView)convertView.findViewById(R.id.love_love_anonymous_item_content);
                convertView.setTag(anonymousViewHolder);
            }else{
                anonymousViewHolder=(AnonymousViewHolder)convertView.getTag();
            }
            anonymousViewHolder.date.setText(love.getCreatedAt());
            anonymousViewHolder.content.setText(love.getContent());
        //不匿名布局
        }else if(currentPosition==UN_ANONYMOUS_TYPE){
            final UnanonymousViewHolder unanonymousViewHolder;
            if(convertView==null){
                unanonymousViewHolder=new UnanonymousViewHolder();
                convertView=inflater.inflate(R.layout.love_love_normal_item,null);
                //初始化控件
                unanonymousViewHolder.avatar=(ImageView)convertView.findViewById(R.id.love_love_normal_item_avatar);
                unanonymousViewHolder.username=(TextView)convertView.findViewById(R.id.love_love_normal_item_username);
                unanonymousViewHolder.date=(TextView)convertView.findViewById(R.id.love_love_normal_item_date);
                unanonymousViewHolder.content=(TextView)convertView.findViewById(R.id.love_love_normal_item_content);
                unanonymousViewHolder.commentCount=(TextView)convertView.findViewById(R.id.love_love_item_normal_reply_count);
                convertView.setTag(unanonymousViewHolder);
            }else {
                unanonymousViewHolder=(UnanonymousViewHolder)convertView.getTag();
            }
            AVUser user=love.getUser();
            unanonymousViewHolder.username.setText(user.get("nickname").toString());
            unanonymousViewHolder.date.setText(love.getCreatedAt());
            unanonymousViewHolder.content.setText(love.getContent());
            unanonymousViewHolder.commentCount.setText(love.getCommentCount()+"");

            //用户头像
            if(null!=user.getAVFile("avatar")){
                int width=mActivity.getResources().getDimensionPixelSize(R.dimen.love_avatar_width);
                //获取缩略图url
                String avatarUrl=user.getAVFile("avatar").getThumbnailUrl(true,width,width);
                UniversalImageLoader.getIntance().displayImage(avatarUrl,unanonymousViewHolder.avatar,UniversalImageLoader.getAvatarOptions(),UniversalImageLoader.getAnimateFirstDisplayListener());
            }else{
                unanonymousViewHolder.avatar.setImageResource(R.mipmap.personal_default_user_icon);
            }

        }
        return convertView;
    }

}
